<?php
/**
 * 后台商品素材的管理
 *
 * Author: Zeon
 * Date: 2018/4/4
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\Product\Asset\AssetStoreRequest;
use App\Http\Requests\Admin\Panx\Product\Asset\AssetUpdateRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Customer\Asset;
use App\Models\Customer\Customer;
use App\Models\Customer\Language;
use App\Models\Panx\Category;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;

class AssetController extends ApiController
{
    const ROUTES = [
        'index'   => 'api.admin.customer.asset.index',
        'create'  => 'api.admin.customer.asset.create',
        'store'   => 'api.admin.customer.asset.store',
        'update'  => 'api.admin.customer.asset.update',
        'destroy' => 'api.admin.customer.asset.destroy',
    ];
    /**
     * Display a listing of the resource.
     * test
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        /** @var Paginator $assets */
        $assets = (new Asset)
            ->where('customer_id', Guard::customer()->getKey())
            ->filterCategory($request->category_id)
            ->columnsKeywordOrLike(['code'], $request->keyword)
            ->latest()
            ->paginate($request->per_page ?: 15);

        $assets->getCollection()->each(function (Asset $asset) {
            $xml = $asset->getOriginal('model_xml');

            $asset->xml_key = $xml;
            if ($asset->isGlass()) {
                $asset->model_glass_android = str_replace('{*}', 'Android', $xml);
                $asset->model_glass_ios = str_replace('{*}', 'IOS', $xml);
            }
        });

        return $this->response(compact('assets'));
    }

    /**
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        /** @var CustomerAdmin $customer_admin */
        $customer_admin = Guard::customerAdmin();
        $language_id = Language::mapCodeToId($customer_admin->language);

        /** @var Customer $customer */
        $customer = $customer_admin->customer;
        $categories = $customer->getCustomerCategories($language_id, true);

        return $this->response(compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     * test
     *
     * @param AssetStoreRequest $request
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(AssetStoreRequest $request)
    {
        $stored = (new Asset)->create([
            'code'        => $request->code,
            'model_xml'   => $request->modelXml(),
            'customer_id' => Guard::customer()->getKey(),
            'category_id' => $request->category_id,
        ]);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     * test
     *
     * @param AssetStoreRequest $request
     * @param  int $id
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(AssetUpdateRequest $request, $id)
    {
        /** @var Asset $asset */
        $asset = (new Asset)
            ->where('customer_id', Guard::customer()->getKey())
            ->find($id);
            $this->checkItemExist($asset, '素材文件');

        $updated = $asset->update([
            'code'        => $request->code,
            'model_xml'   => $request->modelXml(),
            'customer_id' => Guard::customer()->getKey(),
            'category_id' => $request->category_id,
        ]);

        return $this->response(compact('updated', 'asset'));
    }

    /**
     * Remove the specified resource from storage.
     * test
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        /** @var Asset $asset */
        $asset = (new Asset)
            ->where('customer_id', Guard::customer()->getKey())
            ->find($id);
            $this->checkItemExist($asset, '素材文件');

        $deleted = $asset->delete();

        return $this->response(compact('deleted'));
    }
}
